package com.example.cardgame.model

class Card(var suit: String, var rank: String, var isChosen:Boolean = false, var isMatched:Boolean = false)  {
    companion object {
        val rankStrings= arrayOf("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K")
        val validSuits = arrayOf("♥", "♦", "♠", "♣")
    }

    override fun toString(): String {
        return suit + rank
    }

    fun match(otherCards: Array<Card>): Int {
        var score = 0
        if (otherCards.size == 2) {
            val firstCard = otherCards[0]
            val secondCard= otherCards[1]
            //豹子：三张点数相同的牌，最大。
            if(firstCard.rank==rank&&secondCard.rank==rank){
                score=4
            }
            //‌顺金‌：花色相同的顺子，第二大
            else if(firstCard.suit==suit&&secondCard.suit==suit&&isStraight(otherCards)==true){
                score=3
            }
            //花色相同但不是顺子
            else if(firstCard.suit==suit&&secondCard.suit==suit&&isStraight(otherCards)==false){
                score=2
            }
            else{
                score=1
            }

        }
        return score
    }
    fun isStraight(otherCards: Array<Card>): Boolean {
        if (otherCards.size != 2) return false

        val allCards = listOf(this, otherCards[0], otherCards[1])
        val rankIndexes = allCards.map { rankStrings.indexOf(it.rank) }.sorted()

        return (rankIndexes[1] == rankIndexes[0] + 1) && (rankIndexes[2] == rankIndexes[1] + 1)
    }




}